package com.cms.recursoshumanos.ui.menu_comedor;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.ComidaListButtonField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetScrollManager;
import com.cms.recursoshumanos.container.SearchBarManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.ListaComidaMenuScreenHandler;
import com.cms.recursoshumanos.object.MenuComida;
import com.cms.recursoshumanos.object.list.ListaComida;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;
import com.cms.recursoshumanos.utilities.Tool;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;


public class ListaComidaMenuScreen extends RecursosHumanosScreen implements FieldChangeListener 
{

	
	private static ListaComidaMenuScreen instance = null;
	private MenuComida elMenu;
	
	SearchBarManager sbm;
	ListStyleButtonSetScrollManager buttonSet;
	VerticalFieldManager vfm;
	//TwoColumnFieldManager twoColumnManager;
	LabelField lblNombre, lblFechaMenu, lblTipoMenu;
	
	public static ListaComidaMenuScreen getInstance(MenuComida elMenu)
	{
		if(instance == null)
    	{
    		instance = new ListaComidaMenuScreen(elMenu);
    	}
		else
		{
			instance.elMenu = elMenu;
			instance.reset();
		}
    	return instance;
    }
	
	
    private ForegroundManager _foreground;    
    
	public ListaComidaMenuScreen(MenuComida elMenu) 
	{
		super( NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		
		this.elMenu = elMenu;	
		setComponents();
	}
			
	
	protected void makeMenu(Menu menu, int instance) 
	{
		MenuItem refreshButton = new MenuItem("Actualizar", 0, 0) 
        {
			public void run() 
			{
				ListaComidaMenuScreenHandler handler = new ListaComidaMenuScreenHandler();
				handler.refreshListaComidaMenu(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
						UserInfoCache.getInstance().getUsuario().getPassword(),
												elMenu.getFechaMenu(),
												elMenu.getTipoMenu().getTipoMenuId());
			}
		};
		menu.add(refreshButton);
		super.makeMenu(menu, instance);
	}


	public void reset() 
	{
		vfm.deleteAll();
		this.setVFMLista();
		try 
		{
			if(buttonSet.getFieldCount() > 0) 
			{
				//sbm.setFocus();
				buttonSet.deleteAll();
			}
		} 
		catch (Exception e) 
		{
			ShowDialogAlert alert = new ShowDialogAlert("Exception in delete fields...");
			alert.run();
		}
		showListField();;
		//pis = Json.getInstance().getPIs();
	}
	public void setVFMLista()
	{
		/*lblNombre = new LabelField("Menu del Comedor",
				DrawStyle.HCENTER | Field.FIELD_HCENTER);

		lblNombre.setFont(font_plain_8);
		//lblNombre.setMargin( 2 , 2 , 2 , 2 );
		lblNombre.setPadding( Constant.MARGIN_TITULO_VENTANA );*/
		
		lblFechaMenu = new LabelField("Fecha: "+Tool.getInstance().parseDateToString(this.elMenu.getFechaMenu(), 2), 
				DrawStyle.HCENTER | Field.FIELD_HCENTER);
		lblFechaMenu.setFont(font_italic_7);
		
		lblTipoMenu = new LabelField("Tipo: "+ elMenu.getTipoMenu().getNombreTipoMenu(), 
				DrawStyle.HCENTER | Field.FIELD_HCENTER);
		lblTipoMenu.setFont(font_italic_7);
		
		Field separator = new SeparatorField();
        separator.setMargin( 2, 0, 2, 0 );;
        
        vfm.add(lblFechaMenu);
        vfm.add(lblTipoMenu);
        vfm.add(separator);
        
	}
	public void setComponents() 
	{
		setTitle("Menu del Comedor");
		vfm = new VerticalFieldManager( Field.FIELD_HCENTER | VerticalFieldManager.NON_FOCUSABLE  );
		_foreground = new ForegroundManager(NO_VERTICAL_SCROLL);
		//vfm.setPadding(10, 0, 0, 0);
		
		
		
		/*twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 2, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 6, 6, 6, 6 );
        twoColumnManager.setPadding( 2, 2, 2, 2 );
		*/
        buttonSet = new ListStyleButtonSetScrollManager();
        buttonSet.setScreen(this);
        
        sbm = new SearchBarManager(SearchBarManager.STRING_FILTER)
		{
			protected boolean keyChar(char ch, int status, int time) 
			{
				// TODO Auto-generated method stub
				return super.keyChar(ch, status, time);
			}
		};
		this.setVFMLista();
        
        //LabelField tipo_menu = new LabelField("Menu de "+this.elMenu.getTipoMenu().getNombreTipoMenu());
        //String fecha_seleccionada = tools.transformDateFormat(this.elMenu.getFechaMenu());
        
        
        
        showListField();
        
        /*twoColumnManager.add(sbm);
        twoColumnManager.add(vfm);
		twoColumnManager.add(buttonSet);
        _foreground.add( twoColumnManager );
        */
        _foreground.add(sbm);
        _foreground.add(vfm);
        _foreground.add(buttonSet);
        add( _foreground );
	}
	
	public void showListField()
	{
		buttonSet.deleteAll();
		ComidaListButtonField link;
		ListaComida lista_comida = this.elMenu.getListaComida();
		for(int i = 0; i < lista_comida.cantElementListaComida() ; i++) 
		{

			link = new ComidaListButtonField(lista_comida.getComidaAtIndex(i), LabelField.ELLIPSIS) 
			{
			
				protected void paintBackground( Graphics g )
				{
					  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
					  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

				      if( _drawPosition < 0 ) 
				      {
				          // it's like a list field, let the default background be drawn
				          super.paintBackground( g );
				          return;
				      }
				      
				      int oldColour = g.getColor();
				      
				      int focus = COLOR_BACKGROUND_FOCUS;
				      if(_doAction) 
				    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
				      
				      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_BACKGROUND;
				      try 
				      {
				          if( _drawPosition == 0 ) 
				          {
				              // Top
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else if( _drawPosition == 1 ) 
				          {
				              // Bottom 
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				          } 
				          else if( _drawPosition == 2 ) 
				          {
				              // Middle
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else 
				          {
				              // Single
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				          }
				      } 
				      finally 
				      {
				          g.setColor( oldColour );
				      }
				}
			};
			if (i < 4)
			{
				if (lista_comida.getComidaAtIndex(i).getFotoComida() == null)
					link.getFotoComida();
			}
			link.setChangeListener( this );
			buttonSet.add( link );
		}

	}
	public void fieldChanged( Field field, int context ) 
	{
		ScreenEngine.getInstance().goDetailsComida(this.elMenu.getListaComida().getComidaAtIndex(field.getIndex()));
	}	
}


